package com.example.androidlearning.sy6;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTabHost;

import com.example.androidlearning.R;
import com.example.androidlearning.sy6.fragment.FindFragment;
import com.example.androidlearning.sy6.fragment.MyFragment;
import com.example.androidlearning.sy6.fragment.NewsFragment;
import com.example.androidlearning.sy6.fragment.ReadFragment;
import com.example.androidlearning.sy6.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class Sy6Activity02 extends FragmentActivity {

    TabWidget tabs;
    FragmentTabHost tabHost;

    int[] ivTabs;
    String[] tvTabs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sy0602_activity);

        tabs = findViewById(android.R.id.tabs);
        tabHost = findViewById(android.R.id.tabhost);

        List<Class> fragmentList = new ArrayList<>();
        //可以根据需要设置要显示的fragment
        fragmentList.add(MyFragment.class);
        fragmentList.add(MyFragment.class);
        fragmentList.add(MyFragment.class);
        fragmentList.add(MyFragment.class);
        fragmentList.add(MyFragment.class);
        //初始化FragmentTabHost
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        LocalActivityManager l = new LocalActivityManager(this, false);
        tabHost.setup(l);

        ivTabs = new int[]{R.drawable.xinwen, R.drawable.yuedu, R.drawable.shipin, R.drawable.faxian, R.drawable.wode};
        tvTabs = new String[]{"新闻","阅读","视频","发现","我的"};

        for (int i = 0; i < tvTabs.length; i++) {
            //新建TabSpec(小标签)，并setIndicator
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tvTabs[i]).setIndicator(getIndicatorView(i));
            Bundle bundle = new Bundle();
            bundle.putString("title", tvTabs[i]);
            //将TabSpec添加进FragmentTabHost
            //参数1：TabHost.TabSpec， 参数2： 要启动的Fragment的类名， 参数3：要传递的Bundle参数（但是还不知道怎么用）
            tabHost.addTab(tabSpec, fragmentList.get(i), bundle);
        }

        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                TabChange(s);
            }
        });


    }
    //tab切换时，更换相应的tabWidget的颜色
    private void TabChange(String s) {
        int tabIndex = tabHost.getCurrentTab();
        for (int i = 0; i < tvTabs.length; i++) {
//            View vewContent = tabHost.getTabContentView().getChildAt(i);
//            if(vewContent != null){
//                TextView tvTabContent = vewContent.findViewById(R.id.fragment_text);
//                if(tvTabContent != null){
//                    tvTabContent.setText(tvTabs[i]);
//                }
//            }
            View view =  tabHost.getTabWidget().getChildAt(i);
            ImageView ivTab = view.findViewById(R.id.ivTab);
            TextView tvTab = view.findViewById(R.id.tvTab);
            if (i == tabIndex){
                ivTab.setColorFilter(Color.RED);
                tvTab.setTextColor(Color.RED);
            }else {
                ivTab.setColorFilter(Color.GRAY);
                tvTab.setTextColor(Color.GRAY);
            }

        }

    }

    //获取并设置Tab的布局
    private View getIndicatorView(int i) {

        View view = LayoutInflater.from(this).inflate(R.layout.sy0602_tab_item, null, false);
        ImageView ivTab=view.findViewById(R.id.ivTab);
        TextView tvTab=view.findViewById(R.id.tvTab);

        ivTab.setImageResource(ivTabs[i]);
        tvTab.setText(tvTabs[i]);
        if ( i == 0){
            ivTab.setColorFilter(Color.RED);
            tvTab.setTextColor(Color.RED);
        }
        return  view;
    }

}
